package com.F09.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F09.actionform.F0903ActionForm;
import com.F09.dao.FunctionDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.info.FunctionInfo;
import com.util.BuildMessages;
import com.util.CommonTools;

/**
 * 
 * @file F0903Action.java
 * @author Liushimin
 * @since 2008-10-14
 * @version 1.0
 */
public class F0903Action extends BaseAction {

	/**
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		F0903ActionForm actionForm = (F0903ActionForm) form;
		FunctionDAO dao = new FunctionDAO();
		FunctionInfo info = new FunctionInfo();
		String operationType = actionForm.getOperationType();
		String strSubmit = "submit";
		String strQuery = "queryList";
		String strDetail="detail";
		String strList = "list";
		
		if(CommonTools.isNullString(actionForm.getSearchState())){
			actionForm.setSearchState(Constants.STATE_01);
		}
		try {
			if(strSubmit.equals(operationType)){
				info.setFunctionId(actionForm.getFUNCTIONID());
				info.setCode(actionForm.getCODE());
				info.setName(actionForm.getNAME());
				info.setParentId(actionForm.getPARENTID());
				info.setUrl(actionForm.getURL().trim());
				info.setNote(actionForm.getNOTES());
				info.setXh(actionForm.getXH());
				List list = new ArrayList();
				list = dao.selectByCode(info);
				if(list.isEmpty()){ //list == null || list.size() == 0   
					try{
						if("".equals(actionForm.getFUNCTIONID())){	
							dao.insert(info);
						}
						if(!("".equals(actionForm.getFUNCTIONID()))){	
							dao.update(info);
						}
						operationType=strQuery;
						actionForm.setErrorFlag("操作成功!");
					}catch(Exception e){
						actionForm.setErrorFlag("操作失败!");
						e.printStackTrace();
					}
				}else {	
					super.saveMessages(request, BuildMessages.build("common.message"));
					return mapping.findForward("queryList");
				}
			}

			if(Constants.DELETE.equals(operationType)){
				try{
					dao.deleteByKey(CommonTools.null2String(actionForm.getFUNCTIONID()));
					operationType=strQuery;
					actionForm.setErrorFlag("操作成功!");
				}catch(Exception e){
					actionForm.setErrorFlag("操作失败!");
					e.printStackTrace();
				}
				return mapping.findForward("queryList");	
			}
			
			if(Constants.ADD.equals(operationType)){
				actionForm.setFUNCTIONID("");
				actionForm.setCODE("");
				actionForm.setNAME("");
				actionForm.setNOTES("");
				actionForm.setPARENTID("");
				actionForm.setPARENTNAME("");
				actionForm.setURL("");
				actionForm.setXH("");
				return mapping.findForward(strDetail);	
			}
			
			if(Constants.EDIT.equals(operationType)){ 
				Map map = null;
				List list = new ArrayList();
				list = dao.selectById(actionForm.getFUNCTIONID());
				 for(int i = 0; i < list.size();i++){
					map = (Map)list.get(i);	
					actionForm.setFUNCTIONID(CommonTools.null2String((String)map.get("FUNCTIONID")));
					actionForm.setCODE(CommonTools.null2String((String)map.get("CODE")));
					actionForm.setNAME(CommonTools.null2String((String)map.get("NAME")));
					actionForm.setNOTES(CommonTools.null2String((String)map.get("NOTES")));
					actionForm.setPARENTID(CommonTools.null2String((String)map.get("PARENTID")));
					actionForm.setPARENTNAME(CommonTools.null2String((String)map.get("PARENTNAME")));
					actionForm.setURL(CommonTools.null2String((String)map.get("URL")));
					actionForm.setXH(CommonTools.null2String((String)map.get("XH")));
				}
				return mapping.findForward(strDetail);	
			}
			
			if(Constants.BACK.equals(operationType)){
				return mapping.findForward(strList);
			}
			
			if(Constants.QUERYLIST.equals(operationType)){
				actionForm.setList(dao.selectByObject());
				return mapping.findForward(strList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward(strList);
	}
}
